package com.key.win.business.chat.dao;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.key.win.business.chat.model.ImGroupUser;
import com.key.win.business.chat.vo.ImGroupUserFullVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface ImGroupUserDao extends BaseMapper<ImGroupUser> {

    @Select({"SELECT gu.user_Id, gu.id AS id, gu.create_date, gu.create_user_id, gu.create_user_name, gu.enable_flag, gu.update_date, gu.update_user_id, gu.update_user_name, gu.version, g.type, g. STATUS, CASE WHEN gu.`name` IS NOT NULL AND LENGTH(trim(gu.`name`)) > 0 THEN gu.`name` ELSE u.nick_name END AS `name`, gu.group_id, u.sign, u.head_img_url AS avatar, u.phone, u.email FROM im_group g INNER JOIN im_group_user gu ON g.id = gu.group_Id INNER JOIN sys_user u ON gu.user_Id = u.id ${ew.customSqlSegment}"})
    List<ImGroupUserFullVo> findMyGroupUser(@Param("ew") Wrapper<ImGroupUser> queryWrapper);

    @Update({"delete from im_group_user where group_id = #{groupId} and user_Id != (SELECT user_id from im_group where id = #{groupId} )"})
    int deleteGrouUserByGroupId(String groupId);

    @Update({"update im_group_user set enable_flag = 0  where group_id = #{groupId}"})
    int updateEnableFlagByGroupId(String groupId);

}

